package com.example.zhaoyulu.yunzhiapp.fragement;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bumptech.glide.Glide;
import com.example.network.getRetrofitService;
import com.example.network.javaBeans.yzjyinfo;
import com.example.zhaoyulu.yunzhiapp.LoginActivity;
import com.example.zhaoyulu.yunzhiapp.MainActivity;
import com.example.zhaoyulu.yunzhiapp.R;
import com.example.zhaoyulu.yunzhiapp.watchVideoActivity;
import com.example.zhaoyulu.yunzhiapp.webActivity;
import com.example.zhaoyulu.yunzhiapp.zylUtils.GlideRoundTransform;
import com.example.zhaoyulu.yunzhiapp.zylUtils.lunboNetImageHolder;
import com.youth.banner.Banner;

import org.w3c.dom.Text;

import java.util.HashMap;
import java.util.List;

import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by zhaoyulu on 5/9/18.
 */

public class quanziFragment extends Fragment {

    private ConvenientBanner mainquanzi_lunbo;

    private HashMap<String,yzjyinfo> dataMap=new HashMap<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //引用创建好的xml布局
        final View view = inflater.inflate(R.layout.mainquanzifragment,container,false);

//        Button gotoVideo=view.findViewById(R.id.gotoVideo);
//        gotoVideo.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Intent intent=new Intent();
//                intent.setClass(getActivity(),watchVideoActivity.class);
//                startActivity(intent);
//            }
//        });

        getRetrofitService.getService().getyzjyinfo().enqueue(new Callback<List<yzjyinfo>>() {
            @Override
            public void onResponse(retrofit2.Call<List<yzjyinfo>> call, Response<List<yzjyinfo>> response) {
                int i=0;
                initData(response.body());
                initView(view);
            }

            @Override
            public void onFailure(retrofit2.Call<List<yzjyinfo>> call, Throwable t) {

            }
        });

        return view;
    }

    private void initData(List<yzjyinfo> yzjyinfos){
        for(int i=0;i<yzjyinfos.size();i++){
            dataMap.put(yzjyinfos.get(i).getID(),yzjyinfos.get(i));
        }
    }
    private void initView(View view){
        Banner banner=view.findViewById(R.id.banner);
        initBanner(banner);

        ImageView mainquanzi_im_teacher1=view.findViewById(R.id.mainquanzi_im_teacher1);
        TextView mainquanzi_tv_teacher1=view.findViewById(R.id.mainquanzi_tv_teacher1);
        setDate(mainquanzi_im_teacher1,mainquanzi_tv_teacher1,"111");

        ImageView mainquanzi_im_teacher2=view.findViewById(R.id.mainquanzi_im_teacher2);
        TextView mainquanzi_tv_teacher2=view.findViewById(R.id.mainquanzi_tv_teacher2);
        setDate(mainquanzi_im_teacher2,mainquanzi_tv_teacher2,"112");

        ImageView mainquanzi_im_teacher3=view.findViewById(R.id.mainquanzi_im_teacher3);
        TextView mainquanzi_tv_teacher3=view.findViewById(R.id.mainquanzi_tv_teacher3);
        setDate(mainquanzi_im_teacher3,mainquanzi_tv_teacher3,"113");

        ImageView mainquanzi_im_teacher4=view.findViewById(R.id.mainquanzi_im_teacher4);
        TextView mainquanzi_tv_teacher4=view.findViewById(R.id.mainquanzi_tv_teacher4);
        setDate(mainquanzi_im_teacher4,mainquanzi_tv_teacher4,"113");

        ImageView mainquanzi_im_teacher5=view.findViewById(R.id.mainquanzi_im_teacher5);
        TextView mainquanzi_tv_teacher5=view.findViewById(R.id.mainquanzi_tv_teacher5);
        setDate(mainquanzi_im_teacher5,mainquanzi_tv_teacher5,"115");

        String key;

        ImageView personnal1_im1=view.findViewById(R.id.personnal1_im1);
        TextView personnal1_tv1=view.findViewById(R.id.personnal1_tv1);
        key="116";
        Glide.with(getActivity()).load(dataMap.get(key).getPic()).centerCrop().transform(new GlideRoundTransform(getActivity())).into(personnal1_im1);
        personnal1_tv1.setText(dataMap.get(key).getDescription());

        ImageView personnal2_im1=view.findViewById(R.id.personnal2_im1);
        TextView personnal2_tv1=view.findViewById(R.id.personnal2_tv1);
        key="117";
        Glide.with(getActivity()).load(dataMap.get(key).getPic()).centerCrop().transform(new GlideRoundTransform(getActivity())).into(personnal2_im1);
        personnal2_tv1.setText(dataMap.get(key).getDescription());

        ImageView personnal3_im1=view.findViewById(R.id.personnal3_im1);
        ImageView personnal3_tv2=view.findViewById(R.id.personnal3_tv2);
        key="118";
        Glide.with(getActivity()).load(dataMap.get(key).getPic()).centerCrop().transform(new GlideRoundTransform(getActivity())).into(personnal3_im1);
       // Glide.with(getActivity()).load(dataMap.get(key).getPic()).centerCrop().transform(new GlideRoundTransform(getActivity())).into(personnal3_tv2);



    }

    private void initBanner(Banner banner){
        banner.setBannerStyle(Banner.CIRCLE_INDICATOR_TITLE);
        banner.setIndicatorGravity(Banner.CENTER);
        //banner.setBannerTitle(titles);
        banner.isAutoPlay(true) ;
        banner.setDelayTime(5000);
        String[] images= new String[3];
        images[0]=dataMap.get(17+"").getPic();
        images[1]=dataMap.get(18+"").getPic();
        images[2]=dataMap.get(19+"").getPic();
        banner.setImages(images, new Banner.OnLoadImageListener() {
            @Override
            public void OnLoadImage(ImageView view, Object url) {
                Glide.with(getActivity()).load(url).centerCrop().transform(new GlideRoundTransform(getActivity())).into(view);

            }

        });
        //设置点击事件，下标是从1开始
        banner.setOnBannerClickListener(new Banner.OnBannerClickListener() {//设置点击事件
            @Override
            public void OnBannerClick(View view, int position) {
                Intent intent =new Intent(getActivity(),webActivity.class);
                //用Bundle携带数据
                Bundle bundle=new Bundle();
                //传递name参数为tinyphp
                int tempPosition=position+17;
                bundle.putString("url",dataMap.get(tempPosition+"").getUrl());
                intent.putExtras(bundle);
                startActivity(intent);
                //Toast.makeText(getApplicationContext(), "你点击了：" + position, Toast.LENGTH_LONG).show();
            }
        });

    }

    private void setDate(ImageView imageView,TextView textView,final String key){
        Glide.with(getActivity()).load(dataMap.get(key).getPic()).centerCrop().transform(new GlideRoundTransform(getActivity())).into(imageView);
        textView.setText(dataMap.get(key).getDescription());
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent =new Intent(getActivity(),webActivity.class);
                //用Bundle携带数据
                Bundle bundle=new Bundle();
                //传递name参数为tinyphp
                bundle.putString("url",dataMap.get(key).getUrl());
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }


}
